package com.devshephard.test.dc;

import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class kksk {

    public static void main(String[] args) throws Exception {



            List<String> rootPackage = new ArrayList<>();
            rootPackage.add("com.devshephard.test.dc.A");

            while (true){

                List<String> packageNameItemChild = new ArrayList<>();

                for (String pacakgeName : rootPackage) {
                    Class<?> aClass = Class.forName(pacakgeName);
                    for (Field declaredField : aClass.getDeclaredFields()) {
                        packageNameItemChild.add(declaredField.getType().getName());
                    }
                }

                for (String s : rootPackage) {
                    System.out.println(s);
                }

                rootPackage = packageNameItemChild;

            }



              /*  for (int j=0;j<1000;j++){
                    Class<?> aClass = supplierQueueList.get(j);
                    if(aClass.getName().equals(packageName)){
                        Class cl_1 = Class.forName(aClass.getTypeName());
                        for (Field declaredField : cl_1.getDeclaredFields()) {
                            packageName = declaredField.getType().getTypeName();
                            cl_1 = Class.forName(declaredField.getType().getName());
                            break;
                        }
                    }
                }*/


    }





    public List<Class<?>> getSupplierQueueList() throws Exception {
        String basePackageName = this.getClass().getPackage().getName();
        // 获取包的磁盘路径
        String dirPath = getDirPath(basePackageName);
        // 获取磁盘路径下的Java对象
        List<String> classNameList = getClassNameList(dirPath);
        // 获取到class字节码集合
        List<Class<?>> classList = getClassList(classNameList, basePackageName);
        return classList;
    }

    /**
     * 根据包名获取磁盘路径
     *
     * @param basePackageName
     * @return
     * @throws Exception
     */
    private String getDirPath(String basePackageName) throws Exception {
        String path = basePackageName.replace(".", "\\");
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        //获取编译后的class文件夹路径dirPath
        Enumeration<URL> enums = loader.getResources(path);
        String dirPath = "";
        while (enums.hasMoreElements()) {
            URL url = enums.nextElement();
            if (url != null) {
                String protool = url.getProtocol();
                if ("file".equals(protool)) {
                    dirPath = url.getPath();
                    dirPath = URLDecoder.decode(dirPath, "utf-8");
                    //log.info("dirPath=" + dirPath);
                }
            }
        }
        return dirPath;
    }

    /**
     * 获取磁盘路径下的Java对象
     *
     * @param dirPath
     * @return
     */
    private List<String> getClassNameList(String dirPath) {
        List<String> classNameList = new ArrayList<>();
        //根据传入文件夹路径创建File对象
        File dir = new File(dirPath);
        //检查是否为文件夹
        if (dir.isDirectory()) {
            //遍历文件夹内的文件
            for (File f : dir.listFiles()) {
                //获取文件名,并删除后缀 xxx.class
                String fileName = f.getName();
                String[] split = fileName.split("\\.");
                if (split.length != 2) {
                    continue;
                }
                if (!split[1].equals("class")) {
                    continue;
                }
                fileName = fileName.substring(0, fileName.lastIndexOf("."));
                //添加到结果中
                classNameList.add(fileName);
            }
        }
        return classNameList;
    }

    /**
     * 拼接class全路径
     *
     * @param classNames
     * @param basePackageName
     * @return
     */
    private List<Class<?>> getClassList(List<String> classNames, String basePackageName) {
        //将获取到的包内文件名转换为反射对象
        List<Class<?>> classes = new ArrayList<>();
        for (String className : classNames) {
            try {
                classes.add(Class.forName(basePackageName + "." + className));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return classes;
    }


}
